<template>
  <div class="pagination-container" :class="{'hidden': hidden, 'tp-left-p': left}">
    <div v-if="left" class="tp-left"><slot></slot></div>

    <el-pagination
      :background="background"
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      :layout="layout"
      :page-sizes="pageSizes"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :class="{'tp-right': left}"
    />
  </div>
</template>

<script>
import { scrollTo } from '@/utils/scroll-to'

export default {
  name: 'Pagination',
  props: {
    total: {
      required: true,
      type: Number
    },
    page: {
      type: Number,
      default: 1
    },
    limit: {
      type: Number,
      default: 20
    },
    pageSizes: {
      type: Array,
      default() {
        // 此处处理一下分页，因为页大小必须出现在这个数组里面，不然页数错误（原因暂不深究）
        return [ ...new Set( [this.limit, 50, 100, 200, 500] ) ].sort(function(a, b){return a - b});
      }
    },
    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    },
    background: {
      type: Boolean,
      default: true
    },
    autoScroll: {
      type: Boolean,
      default: true
    },
    hidden: {
      type: Boolean,
      default: false
    },
    // 是否显示左侧内容
    left: {
      type: Boolean,
      default: false
    }
  },
  created() {
    
  },
  computed: {
    currentPage: {
      get() {
        return this.page
      },
      set(val) {
        this.$emit('update:page', val)
      }
    },
    pageSize: {
      get() {
        return this.limit
      },
      set(val) {
        this.$emit('update:limit', val)
      }
    }
  },
  methods: {
    handleSizeChange(val) {
      this.$emit('pagination', { page: this.currentPage, limit: val })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
    handleCurrentChange(val) {
      this.$emit('pagination', { page: val, limit: this.pageSize })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.pagination-container {
  background: #fff;
  text-align: center;
  padding: 5px 5px 0px 5px !important;
  margin-top: 0px !important;

  &.hidden {
    display: none;
  }

  .tp-right{
    width: 710px;
    text-align: right;
  }

  .tp-left{
    flex: 1;
    line-height: 32px;
    text-align: left;

    label{
      margin: 0 0px;
      font-size: 12px;
      font-style: normal;
    }
  }
}

.tp-left-p{
  display: flex;
  text-align: center;
}
</style>
